﻿ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MS.Common.Helpers
{
    /// <summary>
    /// 实体属性查找复制
    /// </summary>
    public static class PropertyHelper
    {
        /// <summary>
        /// 返回一个值，该值指示指定的子串是否出现在此字符串中。
        /// 如果 true 参数出现在此字符串中，或者 value 为空字符串 ("")，则为 value；否则为 false。
        /// </summary>
        /// <param name="val"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SFContains(this string val, string value)
        {
            if (val == null)
            {
                return false;
            }
            return val.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0;
        }
        /// <summary>
        /// 扩展object.tostring方法 2022-03-17 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToSafeString(this object obj)
        {
            return obj != null ? obj.ToString() : string.Empty;
        }
        /// <summary>
        /// 字符串转Int 2022-04-08 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            int i;
            if (int.TryParse(str, out i))
                return i;
            else return 0;
        }
        /// <summary>
        /// 判断字符串是否能转成Int 2022-04-08 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInt(this string s)
        {
            int i;
            return int.TryParse(s, out i);
        }

 
        /// <summary>
        ///向目标对象深拷贝数据，存在值跳过
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static void CopyPat<T>(T input, T target)
        {
            List<PropertyInfo> fields = input.GetType().GetProperties().Where(c => c.CanWrite)?.ToList();
            foreach (PropertyInfo field in fields)
            {
                try
                {
                    var inputpro = field.GetValue(input);
                    var targetpro = field.GetValue(target);
                    if (inputpro == null)
                        continue;
                    if (targetpro == null)
                    {
                        field.SetValue(target, inputpro, null);
                        continue;
                    }
                    Type gettype = inputpro.GetType();
                   
                        if (!string.Empty.Equals(inputpro.ToString()) && string.Empty.Equals(targetpro.ToString()))
                            field.SetValue(target, inputpro, null);
                    
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 从多个实体中，查找属性值 2023-02-15 
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="objList"></param>
        /// <returns></returns>
        public static T FindProValue<T>(string PropertyName, params object[] objList)
        {
            T findValue = default(T);
            foreach (object obj in objList)
            {
                System.Reflection.PropertyInfo p1 = obj.GetType().GetProperty(PropertyName);
                if (p1 != null)
                {
                    object obj1 = p1.GetValue(obj, null);
                    if (obj1 != null)
                    {
                        try
                        {
                            findValue = (T)obj1;//拆箱
                        }
                        catch (Exception ex)
                        {//指定的拆箱类型不对吧
                        }
                    }
                    break;//找到了该属性
                }
            }
            return findValue;
        }
        /// <summary>
        /// 从多个实体中，查找属性赋值 2023-02-15 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="PropertyName">属性名</param>
        /// <param name="Val">属性值</param>
        /// <param name="objList">实体中查找赋值</param>
        /// <returns></returns>
        public static void FindProSetValue(string PropertyName, object Val, params object[] objList)
        {
            foreach (object obj in objList)
            {
                if (obj == null)
                    continue;
                System.Reflection.PropertyInfo p1 = obj.GetType().GetProperty(PropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (p1 != null && p1.CanWrite)
                {
                    try
                    {
                        p1.SetValue(obj, Val, null);
                    }
                    catch (Exception ex)
                    {
                    }
                    break;//找到了该属性
                }
            }
        }
    }
}
